const pool = require("./utils/pool");

pool.query("select * from news_a", (error, result) => {
  if (error) {
    res.send(Response.error(500, error));
    throw error;
  }
  'insert into tableName ()'
  let obj = {};
  result.forEach((item, index) => {
    let str = item.content;
    while (true) {
      let i = str.indexOf('src="');
      if (i == -1) {
        break;
      } else {
        let endIndex = str.indexOf('"', i + 5);
        let ss = "";
        for (let u = i; u <= endIndex; u++) {
          ss += str[u];
        }
        str = str.replace(ss, "");
      }
    }
    item.content = str;
    

    //   console.log(item.content);
    // content =
    obj = {
        title:item.title,
        date:item.date,
        date_time:item.date_tiem,
        source:item.source,
        type:item.type,
        content:item.content
    }
    
    pool.query("insert into news set ?",obj, (error, result) => {
       if(error){
        throw error
       }
       if(result.affectedRows > 0){
        console.log(obj);
       }else {
        console.log('失败');
       }

    });
    // console.log(obj);
  });
});
